package com.huazai.out_school_manager.Servlet;

import com.huazai.out_school_manager.Util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "user",value = "/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userid = (String) request.getAttribute("userid");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();


        pw.print("<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>出校申请总览</title>\n" +
                "    <script type='text/javascript'>\n" +
                "      function del(applyID) {\n" +
                "        if(window.confirm('好哥们，确定要删除吗？（删除了之后可是不可恢复的哦）')) {\n" +
                "            document.location.href = 'delete?applyID=' + applyID;\n" +
                "        }\n" +
                "      }\n" +
                "    </script>\n" +
                "</head>\n");

//        在默认不配置action时，默认是get的请求方式。
        pw.print("<form action='add'>");
        pw.print("<h1>" + userid+ " 普通用户出校申请管理</h1>");
        pw.print("<h1>按照格式来填写哦</h1>");

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtil.getConnectDB();
            String sql = "select * from submitinfo where Huserid = ? order by Hsubmit desc";
            ps = conn.prepareStatement(sql);

//            System.out.println(userid);
//            这里根据userid来分别是谁发出的请求
            ps.setString(1, userid);

            rs = ps.executeQuery();
            pw.print("<table align='center' border='1' width='70%' >");
            pw.print("<tr>" +
                    "<th>提交用户ID</th>" +
                    "<th>提交时间</th>" +
                    "<th>申请时长（小时）</th>" +
                    "<th>审批状态（P,R,A）</th>" +
                    "<th>执行操作</th>" +
                    "</tr>");
//            if(!rs.next()) {
//                pw.print("<h1 align='center'>现在还没有任何申请哦</h1>");
//            }
            /**
             * 不知道什么原因代码会被吞掉，学学前端或许就明白了吧。
             */

            while( rs.next() ) {
                String JudgeUserid = rs.getString("Huserid");
                String submitTime = rs.getString("Hsubmit");
                String applyID = rs.getString("applyID");

                pw.print("<tr>");
                pw.print("<td align='center'>" + JudgeUserid + "</td>");
                pw.print("<td align='center'>" + submitTime + "</td>");
                pw.print("<td align='center'>" + rs.getString("Htime") + "</td>");
                pw.print("<td align='center'>" + rs.getString("Hresult") + "</td>");
                pw.print("<td align='center'> <a href=\"javascript:void(0)\" onclick='del(\""+ applyID + "\")'>删 除</a></td>\n");

                pw.print("</tr>");
            }
            pw.print("</table>");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }

        for (int i = 0; i < 5; i++) {
            pw.print("<br>");
        }
        pw.print("<table align='center'>\n" +
                "    <tr>\n" +
                "        <td><label for='userid'>你的账户ID：</label></td>\n" +
                "        <td><input type='text' name='userid' id='userid' value='"+ userid +"'readonly/></td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td><label for='dateTime'>预出校时间：</label></td>\n" +
                "        <td><input type='datetime-local' name='dateTime' id='dateTime' /></td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td><label for='outTime'>出校时长：</label></td>\n" +
                "        <td><input type='text' name='outTime' id='outTime' /></td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td colspan='3'><input type='submit' value='提交申请' /></td>\n" +
                "    </tr>\n" +
                "</table>");
        pw.print("</form>");
    }

}